package com.zust.data.web.controller;

import com.zust.data.domain.param.UserLoginParam;
import com.zust.data.domain.param.UserRegParam;
import com.zust.data.dto.ResponseDto;
import com.zust.data.entity.DlChart;
import com.zust.data.serivce.ChartService;
import com.zust.data.serivce.ColumnService;
import com.zust.data.serivce.DataService;
import com.zust.data.serivce.UserService;
import com.zust.data.dto.SessionInfo;
import com.zust.data.util.StrUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static com.zust.data.util.StrUtil.chart_img_name;

/**
 * @author senselyan
 * @date 2018/5/3 19:30
 */
@Controller
@RequestMapping("")
public class UserController {

    @Inject
    private UserService userService;

    @Inject
    private DataService dataService;

    @Inject
    private ColumnService columnService;


    @Inject
    private ChartService chartService;

    @RequestMapping("/user/login")
    public String login() {
        return "user/login";
    }

    @RequestMapping("/user/set")
    public String set(HttpServletRequest request, HttpSession session) {
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
        session.setAttribute(StrUtil.session_val_menu_type, 2);
        request.setAttribute("user", userService.getUserInfo(sessionInfo.getId()).getData());
        return "user/set";
    }

    @RequestMapping("/user/reg")
    public String reg(UserRegParam userRegParam) {
        return "user/reg";
    }


    @RequestMapping("/user/index")
    public String index(HttpServletRequest request, HttpSession session) {
        session.setAttribute(StrUtil.session_val_menu_type, 1);
        return "user/set";
    }

    @RequestMapping("/user/message")
    public String message(HttpServletRequest request, HttpSession session) {
        session.setAttribute(StrUtil.session_val_menu_type, 3);
        return "user/message";
    }

    @RequestMapping("/user/data/id")
    public String dataId() {
        return "user/one_data";
    }

    @RequestMapping("/user/table")
    public String table() {
        return "user/table";
    }


    @RequestMapping("/user/home")
    public String home() {
        return "user/home";
    }


    @RequestMapping("/user/logout")
    public String logout(UserLoginParam userLoginParam,
                         HttpServletRequest request, HttpSession session) {
        session.setAttribute("sessionInfo", null);
        return "user/login";
    }


    @RequestMapping("/user/data")
    public String data(HttpServletRequest request, HttpSession session) {
        session.setAttribute(StrUtil.session_val_menu_type, 0);
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");

        request.setAttribute("tableData", dataService.listByUserIdAndType(sessionInfo.getId(), 0));
        request.setAttribute("jsonData", dataService.listByUserIdAndType(sessionInfo.getId(), 1));
        return "data/data";
    }

    @RequestMapping("/data/save")
    public String saveData(HttpServletRequest request, HttpSession session) {
        session.setAttribute(StrUtil.session_val_menu_type, 0);
        return "data/save";
    }

    @RequestMapping("/data/{dataId}/update")
    public String updateData(HttpServletRequest request, HttpSession session,
                             @PathVariable String dataId) {
        session.setAttribute(StrUtil.session_val_menu_type, 0);
        request.setAttribute("dataInfo", dataService.info(dataId));
        request.setAttribute("columns", columnService.listByDataId(dataId));
        return "data/update";
    }

//    @RequestMapping("/data/{dataId}/info")
//    public String infoData(HttpServletRequest request, HttpSession session,
//                           @PathVariable String dataId) {
//        session.setAttribute(StrUtil.session_val_menu_type, 0);
//        return "data/info";
//    }

    @RequestMapping("/data/{dataId}/info")
    public String oneData(HttpServletRequest request, HttpSession session,
                          @PathVariable String dataId) {
        session.setAttribute(StrUtil.session_val_menu_type, 0);

        request.setAttribute("dataInfo", dataService.info(dataId));
        request.setAttribute("columns", columnService.listByDataId(dataId));
        return "data/info";
    }


    /**
     * ----------------------------------chart-----------------------------------
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/chart/demo")
    public String chartDemo(HttpServletRequest request, HttpSession session) {
        session.setAttribute(StrUtil.session_val_menu_type, 4);
        return "chart/demo";
    }

    @RequestMapping("/chart/create")
    public String create(HttpServletRequest request, HttpSession session) {
        session.setAttribute(StrUtil.session_val_menu_type, 4);
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
        request.setAttribute("tableData", dataService.listByUserIdAndType(sessionInfo.getId(), 0));
        request.setAttribute("jsonData", dataService.listByUserIdAndType(sessionInfo.getId(), 1));
        request.setAttribute("chartImgs", chart_img_name);
        return "chart/create";
    }

    @RequestMapping("/chart/{chartId}/info")
    public String oneChart(HttpServletRequest request, HttpSession session,
                           @PathVariable String chartId) {
        session.setAttribute(StrUtil.session_val_menu_type, 4);
        DlChart dlChart = chartService.info(chartId);
        request.setAttribute("chartInfo", dlChart);
        request.setAttribute("columns", columnService.listByDataId(dlChart.getDataId()));
        request.setAttribute("dataInfo", dataService.info(dlChart.getDataId()));
        return "chart/" + dlChart.getPageName();
    }

    @RequestMapping("/chart/{chartId}/update")
    public String updateChart(HttpServletRequest request, HttpSession session,
                           @PathVariable String chartId) {
        session.setAttribute(StrUtil.session_val_menu_type, 4);
        DlChart dlChart = chartService.info(chartId);
        request.setAttribute("dataInfo", dataService.info(dlChart.getDataId()));
        request.setAttribute("chartInfo", dlChart);
        request.setAttribute("columns", columnService.listByDataId(dlChart.getDataId()));
        return "chart/update";
    }

    @RequestMapping("/chart/list")
    public String listChart(HttpServletRequest request, HttpSession session) {
        session.setAttribute(StrUtil.session_val_menu_type, 4);
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
        request.setAttribute("chartInfo", chartService.list(sessionInfo.getId()));
        return "/chart/list";
    }

}


